﻿ 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.BLL;
using GCELibrary.Current;

namespace ZF.WS.UserControls.Index
{
    public partial class MercList : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            

            var typeList = DictionaryConfig.MTypeJsonList.Where(p=> p.MType.Name!="虚拟商品");
            this.lvTitle.DataSource = typeList.Where(p=>p.MType.ParentId==0).Select(p => new
            {
                Key = p.MType.MerchandiseTypeId,
                Text = p.MType.Name,
            }); 
            this.lvTitle.DataBind();
            this.lvPType.DataSource = DictionaryConfig.MTypeJsonList.Where(p => p.MType.ParentId == 0).Select(p => new
            {
                Key = p.MType.MerchandiseTypeId,
                Text = p.MType.Name,
                ParentId = p.MType.ParentId,
            });
            this.lvPType.DataBind();

            var typelist = DictionaryConfig.MTypeJsonList.Select(p => new
            {
                ParentId = p.MType.ParentId,
                Key = p.MType.MerchandiseTypeId,
                Text = p.MType.Name,
            }).ToList();
            this.litTypeJson.Text = Newtonsoft.Json.JsonConvert.SerializeObject(typelist);
            
            using (var dc = SiteInfo.CreateDBDataContext())
            {


                Dictionary<object, List<object>> dicType = null;

                var cacheKey = "_index_MercList";
                var obj = HttpCache.GetCache(cacheKey);
                if (obj != null)
                {
                    dicType = obj as Dictionary<object, List<object>>;
                }
                else
                {
                    dicType = new Dictionary<object, List<object>>();
                    foreach (var type in typeList)
                    {
                        var cids = typeList.Where(p => p.MType.ParentId == type.MType.MerchandiseTypeId).Select(p => p.MType.MerchandiseTypeId)
                            .ToArray();
                        var list = dc.SP_Merchandise.Where(p => !p.IsDel)
                            .Where(p => p.MerchandiseTypeId == type.MType.MerchandiseTypeId || cids.Contains(p.MerchandiseTypeId))
                            .OrderByDescending(p => p.MerchandiseId)
                            .OrderByDescending(p => p.OrderID)
                            .OrderByDescending(p => p.IsHot)
                            .OrderByDescending(p => p.IsRecommend)
                            .Take(10).ToList().Select(p =>
                             (object)new
                             {
                                 Merc = p,
                                 Photo = p.FirstPhoto,
                             }).ToList();
 
                        list = list.Take(10).ToList();
                        if (type.MType.ParentId == 0)
                        {
                            if (list.Count > 0)
                                dicType[type.MType.MerchandiseTypeId] = list;
                            else
                                dicType[type.MType.MerchandiseTypeId] = new List<object>();
                        }
                         
                    }
                    SiteInfo.Instance.SetPageCache(cacheKey,dicType);
                }

                this.lvList.DataSource = dicType;
                this.lvList.DataBind();

                var hotList = dc.SP_Merchandise.Where(p =>!p.IsDel).OrderBy(p=>p.IsPromotions).OrderByDescending(p=>p.Hits).Take(3).ToList().Select(p => new
                {
                    Merc = p,
                    Photo = p.FirstPhoto,
                }).ToList();
                this.lvHot.DataSource = hotList;
                this.lvHot.DataBind();


                this.lvFitup.DataSource = dc.I_Fitup.Where(p => p.TypeId == 2).OrderByDescending(p => p.AddTime).Take(4).ToList();
                this.lvFitup.DataBind();
            }
            
        }
    }
}